package cn.com.gzheroli.core.util;

import java.beans.BeanInfo;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.ibatis.jdbc.SQL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.gzheroli.core.common.entity.TableAndColumns;
import cn.com.gzheroli.core.common.model.common.SqlContext;

public class HeroSqlUtils {
	 /** 日志对象 */
    private static final Logger LOG = LoggerFactory.getLogger(HeroSqlUtils.class);

    
    /**
     * 
    * @Title: buildBeetlInsertSql 
    * @Description: 构建insert语句 
    * @param @param tableName
    * @param @param paras
    * @param @return    设定文件 
    * @return Map<String,Object>    返回类型 
    * @throws
     */
    public static Map<String,Object> buildBeetlInsertSql(String tableName,Map<String,Object> paras ) {
    	Map<String,Object> sqlMap=new HashMap<String,Object>();
    	SQL sql=new SQL();
    	 if(tableName.indexOf("_")<0) {
    	tableName=NameHandler.getTableName(tableName);
    	 }
    	sql.INSERT_INTO(tableName);
        if(!paras.containsKey("id")||!paras.containsKey("id_")){
        	String key="id";
        	String columnName=NameHandler.getColumnName(key);
            if(tableName.startsWith("Act")&&!columnName.endsWith("_")) {
            	columnName=NameHandler.getColumnName(key+"_");
            }
            sql.INTO_VALUES("#"+key+"#");
        	sql.INTO_COLUMNS(columnName);
        	sqlMap.put(key, IdWorker.getFlowIdWorkerInstance().nextIdStr());
        }
        	
        for (String key : paras.keySet()) {  
            Object value =paras.get(key);
            if((key.equalsIgnoreCase("id")||key.equalsIgnoreCase("id_"))&&(value == null||value.toString().trim().equals(""))) {
				value=IdWorker.getFlowIdWorkerInstance().nextIdStr();
            }
            if (value == null) {
                continue;
            }
            String columnName=NameHandler.getColumnName(key);
            if(tableName.startsWith("Act")&&!columnName.endsWith("_")) {
            	columnName=NameHandler.getColumnName(key+"_");
            }
            sql.INTO_VALUES("#"+key+"#");
        	sql.INTO_COLUMNS(columnName);
        	sqlMap.put(key, value);
        }
        sqlMap.put("sql", sql.toString());
        return sqlMap;
    }
    /**
     * 构建更新sql
     * 
     * @param entity
     * @param nameHandler
     * @return
     */
    public static Map<String,Object> buildBeetlUpdateSql(String tableName,Map<String,Object> paras,Map<String,Object> whereMap) {
    	Map<String,Object> sqlMap=new HashMap<String,Object>();
    	Map<String,Object> paraMap=new HashMap<String,Object>();
    	SQL sql=new SQL();
        if(tableName.indexOf("_")<0) {
        tableName=NameHandler.getTableName(tableName);
        }
        sql.UPDATE(tableName);
        for (String key : paras.keySet()) { 
        	Object value =paras.get(key);
            if (value == null) {
                continue;
            }
            String columnName=NameHandler.getColumnName(key);
            if(tableName.startsWith("Act")&&!columnName.endsWith("_")) {
            	columnName=NameHandler.getColumnName(key+"_");
            }
            sql.SET(columnName+"=#"+key+"#");
            paraMap.put(key, value);
        }
        if(whereMap!=null&&!whereMap.isEmpty()){
        	for (String key : whereMap.keySet()) { 
        		
        		String columnName=NameHandler.getColumnName(key);
                if(tableName.startsWith("Act")&&!columnName.endsWith("_")) {
                	columnName=NameHandler.getColumnName(key+"_");
                }
                sql.WHERE(columnName+"=#"+key+"#");
        		Object value =whereMap.get(key);
                paraMap.put(key.toString(), value);
            }
        }else{
        	String columnName=NameHandler.getColumnName("id");
            if(tableName.startsWith("Act")&&!columnName.endsWith("_")) {
            	columnName=NameHandler.getColumnName("id"+"_");
            }
            sql.WHERE(columnName+"=#id#");
            paraMap.put("id", paraMap.get("id")==null?paraMap.get("id"):paraMap.get("id_"));
        }
        
        
        sqlMap.put("sql", sql.toString());
        sqlMap.putAll(paraMap);
    	//sqlMap.put("paraMap", paraMap);
        return sqlMap;
    }
    /**
     * 
    * @Title: buildBeetlDeleteSql 
    * @Description: 删除语句生成 
    * @param @param tableName
    * @param @param whereMap
    * @param @return    设定文件 
    * @return Map<String,Object>    返回类型 
    * @throws
     */
    public static Map<String,Object> buildBeetlDeleteSql(String tableName,Map<String,Object> whereMap ){
    	Map<String,Object> sqlMap=new HashMap<String,Object>();
    	SQL sql=new SQL();
        if(tableName.indexOf("_")<0) {
        tableName=NameHandler.getTableName(tableName);
        }
        sql.DELETE_FROM(tableName);
        if(whereMap!=null&&!whereMap.isEmpty()){
        	for (String key : whereMap.keySet()) { 
        		
        		String columnName=NameHandler.getColumnName(key);
                if(tableName.startsWith("Act")&&!columnName.endsWith("_")) {
                	columnName=NameHandler.getColumnName(key+"_");
                }
                sql.WHERE(columnName+"=#"+key+"#");
        		Object value =whereMap.get(key);
        		sqlMap.put(key.toString(), value);
            }
        	sqlMap.put("sql", sql.toString());
        	return sqlMap;
        }else{
        	sqlMap.put("sql", "");
        	return sqlMap;
        }
    }
    /**
     * 获取属性值
     *
     * @param readMethod
     * @param entity
     * @return
     */
    private static Object getReadMethodValue(Method readMethod, Object entity) {
        if (readMethod == null) {
            return null;
        }
        try {
            if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                readMethod.setAccessible(true);
            }
            return readMethod.invoke(entity);
        } catch (Exception e) {
            LOG.error("获取属性值失败", e);
            throw new RuntimeException(e);
        }
    }
}
